<style>
    #test + .layui-table-view .layui-table-body tbody > tr > td {
        padding: 0;
    }

    #test + .layui-table-view .layui-table-body tbody > tr > td > .layui-table-cell {
        height: 150px;
    }
</style>
<body>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <select name="field_name" lay-verify="required" required>
                            <option value="uid">用户ID</option>
                            <option value="account">账号</option>
                        </select>
                    </div>
                    <div class="layui-inline">
                        <input name="field_value" class="layui-input" placeholder="输入搜索内容"/>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <select name="status">
                                <option value="">审核状态</option>
                                <option value="0">待审核</option>
                                <option value="1">通过</option>
                                <option value="2">驳回</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <select name="type">
                                <option value="">提现类型</option>
                                <option value="bankCard">银行卡</option>
                                <option value="usdt">usdt</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <select name="date_type" lay-verify="required" required>
                            <option value="created_at">创建时间</option>
                            <option value="updated_at">审核时间</option>
                        </select>
                    </div>
                    <div class="layui-inline">
                        <input type="text" autocomplete="off" id="startDate" name="start_date" class="layui-input"
                               placeholder="开始日期">
                    </div>
                    <div class="layui-inline">
                        <input type="text" autocomplete="off" id="endDate" name="end_date" class="layui-input"
                               placeholder="结束日期">
                    </div>
                    <div class="layui-inline">&emsp;
                        <button class="layui-btn icon-btn" lay-filter="search" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="test" lay-filter="test"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="barDemo">
    {{# if(d.status == 0) { }}
    <a class="layui-btn layui-btn-success layui-btn-xs" lay-event="pass">通过</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="reject">驳回</a>
    {{# } }}
<!--    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="del">删除</a>-->
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="saveRejectDialog">
    <form id="saveRejectForm" lay-filter="saveRejectForm" class="layui-form model-form">
        <input type="hidden" name="id">
        <input type="hidden" name="status" value="2">
        <div class="layui-form-item">
            <label class="layui-form-label">驳回理由</label>
            <div class="layui-input-block">
                <textarea name="remark" placeholder="请输入驳回理由" autocomplete="off" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="saveRejectSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script type="text/javascript" src="__TPL_RESOURCE_BACKEND__/libs/layui/layui.js"></script>
<script type="text/javascript" src="__TPL_RESOURCE_BACKEND__/js/common.js"></script>
<script>
    layui.use(['layer', 'form', 'table', 'admin','laydate'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var admin = layui.admin;
        var laydate = layui.laydate;

        const startTime = new Date(new Date().setDate(new Date().getDate() - 30));
        startTime.setHours(0, 0, 0, 0);
        var startDate = startTime.toISOString().split('T')[0] + ' 00:00:00';

        const endTime = new Date(new Date().setDate(new Date().getDate() + 2));
        endTime.setHours(0, 0, 0, 0);
        var endDate = endTime.toISOString().split('T')[0] + ' 00:00:00';

        laydate.render({
            elem: '#startDate',
            type: 'datetime',
            value: startDate
        });

        laydate.render({
            elem: '#endDate',
            type: 'datetime',
            value: endDate
        });

        /* 渲染表格 */
        var insTb = table.render({
            elem: '#test',
            url: "{{:url('Withdraw/index')}}",
            page: {
                limit: 20
            },
            where: {
                start_date: startDate,
                end_date: endDate
            },
            method: 'post',
            contentType: 'application/json',
            toolbar: true,
            cols: [[
                {field: 'id', title: 'ID', width: 50},
                {field: 'user_account_txt', title: '账号', width: 120},
                {field: 'origin_money', title: '提现金额', width: 120},
                {field: 'withdraw_fee', title: '手续费(%)', width: 100},
                {field: 'money', title: '提现金额(扣除手续费)', width: 170},
                {
                    title: '状态', templet: function (d) {
                        return [
                            '<span class="layui-badge layui-badge-gray">待审核</span>',
                            '<span class="layui-badge layui-badge-green">通过</span>',
                            '<span class="layui-badge layui-badge-red">驳回</span>'
                        ][d.status];
                    }, align: 'center', width: 80
                },
                {
                    title: '卡信息', templet: function (d) {
                        if(d.type == 'bankCard') {
                            return '<span class="layui-badge layui-badge-blue">银行卡</span><br>'+
                                '<span>姓名：' + d.username + '</span><br>' +
                                '<span>账号：' + d.number + '</span><br>' +
                                '<span>开户行：' + d.bank_name + '</span><br>' +
                                '<span>支行：' + d.bank_branch_name + '</span>'
                        }
                        if(d.type == 'usdt') {
                            return '<span class="layui-badge layui-badge-red">usdt</span><br>'+
                            '<span>地址：' + d.address + '</span><br>' +
                            '<span>网络：' + d.network + '</span>'
                        }
                    }, align: 'left'
                },
                {
                    title: '时间信息', templet: function (d) {
                        return '<span>创建：' + d.created_at + '</span><br>' +
                            '<span>审核：' + d.updated_at + '</span>'
                    }, align: 'left'
                },
                {field: 'remark', title: '备注'},
                {title: '操作', toolbar: '#barDemo', align: 'center', width: 120}
            ]]
        });

        /* 表格搜索 */
        form.on('submit(search)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 表格工具条点击事件 */
        table.on('tool(test)', function (obj) {
            if (obj.event === 'pass') {
                check(obj.data, 1);
            } else if (obj.event === 'reject') {
                showSaveRejectModel(obj.data);
            } else if (obj.event === 'del') {
                doDel(obj);
            }
        });

        /* 显示充值弹窗 */
        function showSaveRejectModel(mData) {
            admin.open({
                type: 1,
                title: '驳回说明',
                area: ['500px', '300px'],
                maxmin: true,
                resize: true,
                content: $('#saveRejectDialog').html(),
                success: function (layero, dIndex) {
                    form.val('saveRejectForm', {
                        id: mData.id
                    });
                    var url = "{{:url('Withdraw/check')}}";
                    // 表单提交事件
                    form.on('submit(saveRejectSubmit)', function (data) {
                        var loading = layer.load(3);

                        $.ajax({
                            url: url,
                            type: 'post',
                            data: data.field,
                            success: function (res) {
                                if (res.code != 0) {
                                    layer.msg(res.msg, {icon: 5, time: 1000});
                                } else {
                                    layer.close(dIndex);
                                    layer.msg(res.msg, {icon: 1, time: 1500});
                                    insTb.reload();
                                }
                                layer.close(loading);
                            }, error: function () {
                                layer.msg('网络错误请稍后重试', {icon: 5, time: 1000});
                                layer.close(loading);
                            }
                        });
                        return false;
                    });
                }
            });
        }


        /* 审核 */
        function check(obj, status) {
            layer.confirm('确定要审核选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loading = layer.load(3);
                $.ajax({
                    url: "{{:url('Withdraw/check')}}",
                    type: 'post',
                    data: {id: obj.id, status: status},
                    success: function (res) {
                        if (res.code != 0) {
                            layer.msg(res.msg, {icon: 5, time: 1000});
                        } else {
                            layer.msg(res.msg, {icon: 1, time: 1500});
                            insTb.reload();
                        }
                        layer.close(loading);
                    }, error: function () {
                        layer.msg('网络错误请稍后重试', {icon: 5, time: 1000});
                        layer.close(loading);
                    }
                });
                return false;
            });
        }

        /* 删除 */
        function doDel(obj) {
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loading = layer.load(3);
                $.ajax({
                    url: "{{:url('Withdraw/del')}}",
                    type: 'post',
                    data: {id: obj.data.id},
                    success: function (res) {
                        if (res.code != 0) {
                            layer.msg(res.msg, {icon: 5,time:1000});
                        } else {
                            layer.msg(res.msg, {icon: 1,time:1500});
                            insTb.reload();
                        }
                        layer.close(loading);
                    }, error: function () {
                        layer.msg('网络错误请稍后重试', {icon: 5,time:1000});
                        layer.close(loading);
                    }
                });
                return false;
            });
        }
    });
</script>
</body>
</html>
